// pages/arrange/arrange.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cinemaId: '',
    cinemaName: '',
    address: '',
    movieName: '',
    currentSelectId: '',
    wantMovieInfo: '',
    dateList: [],
    currentSelect: 0,
    movieArrange: [],


    moveParams: {
      scrollLeft: 0, // scroll-view滚动的距离,默认为0,因为没有触发滚动
      subLeft: '', //点击元素距离屏幕左边的距离
      subHalfWidth: '', //点击元素的宽度一半
      screenHalfWidth: '' //屏幕宽度的一半
    },

    currentMovieInfo: {
      id: '',
      still: '',
      movieName: '',
      movieType: '',
      cast: '',
      last: ''
    },
    movieList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    wx.request({
      url: 'http://localhost:8081/api/movie/wxGetMovieListByCinemaId',
      method: 'GET',
      data: {
        cinemaId: options.cinemaId
      },
      success: function (res) {
        that.setData({
          movieList: res.data.result
        })
        for(var i = 0; i < that.data.movieList.length; i++){
          if (wx.getStorageSync('selectMovieId') == that.data.movieList[i].id) {
            that.setData({
              currentMovieInfo : that.data.movieList[i]
            })
          }
        }
      }
    });
    let list = that.getDateList()
    that.setData({
      currentSelect : options.currentSelect,
      cinemaId: options.cinemaId,
      cinemaName: options.cinemaName,
      address: options.address,
      currentSelectId: wx.getStorageSync('selectMovieId'),
      ['currentMovieInfo.movieName']: wx.getStorageSync('selectMovieName'),
      ['currentMovieInfo.id']: wx.getStorageSync('selectMovieId'),
      dateList: list
    });

    that.updateMovieArrange()
  },

  // 跳转至选座页面
  toSeat(e) {
    console.log(e)
    var that = this
    wx.navigateTo({
      url: '../selectSeat/selectSeat?dateInfo=' + that.data.dateList[that.data.currentSelect].currentDate,
    }),
    wx.setStorageSync('movieStill', that.data.currentMovieInfo.still)
    wx.setStorageSync('selectMovieArrangeInfo', e.currentTarget.dataset.toseatinfo)
    // wx.setStorageSync('selectMovieHallName', that.data.movieArrange.movieHallName)
  },

  // 时间切换监听
  changeSelect(e) {
    var that = this
    that.setData({
      currentSelect: e.currentTarget.dataset.currentnum
    })
    that.updateMovieArrange(e)
  },

  // 更新电影场次信息
  updateMovieArrange(e) {
    var that = this
    var tempNowTime
    if (that.data.dateList[that.data.currentSelect] == undefined) {
      return
    }
    if (e != undefined) {
      tempNowTime = '2023'+'-'+that.data.dateList[e.currentTarget.dataset.currentnum].currentDate
    }else {
      tempNowTime = '2023' + '-' + that.data.dateList[that.data.currentSelect].currentDate
    }
    wx.request({
      url: 'http://localhost:8081/api/movieArrange/wxGetMovieArrangeListByCinemaIdAndMovieName',
      data: {
        movieName : that.data.currentMovieInfo.movieName,
        nowTime : tempNowTime,
        cinemaId : that.data.cinemaId
      },
      method: "GET",
      "content-type":"application/json",
      success: function(res){
        that.setData({
          movieArrange : res.data.result
        })
      }
    })
  },

  getDateList() {
    var that = this
    var list = []
    var now = new Date()
    var month = now.getMonth() + 1;
    var day = now.getDate();
    var keepDay = now.getDate();
    if (month < 10) {
      month = '0' + month;
    };
    

    var today
    for(var i = 0;i < 5; i++){
      if ((keepDay + i) < 10) {
        day = '0' + (keepDay + i);
      }else {
        day = keepDay + i
      }
      today = month + '-' + day
      list.push({"currentNum":i,"currentDate":today})
    }
    return list
  },

  getRect(ele) { //获取点击元素的信息,ele为传入的id
    var that = this;
    wx.createSelectorQuery().select(ele).boundingClientRect(function (rect) {
      let moveParams = that.data.moveParams;
      moveParams.subLeft = rect.left;
      moveParams.subHalfWidth = rect.width / 2;
      that.moveTo();
    }).exec()
  },


  // 切换当前电影选择
  switchMovie(e) {
    let ele = 'ele' + e.target.dataset.movieinfo.id
    var that = this
    that.setData({
      currentMovieInfo: e.currentTarget.dataset.movieinfo,
    })
    that.updateMovieArrange()
    that.getRect('#' + ele);
  },

  scrollMove(e) {
    let moveParams = this.data.moveParams;
    moveParams.scrollLeft = e.detail.scrollLeft;
    this.setData({
      moveParams: moveParams
    })
  },

  moveTo() {
    let subLeft = this.data.moveParams.subLeft;
    let screenHalfWidth = 160;
    let subHalfWidth = this.data.moveParams.subHalfWidth;
    let scrollLeft = this.data.moveParams.scrollLeft;
    let distance = subLeft - screenHalfWidth + subHalfWidth;
    scrollLeft = scrollLeft + distance;
    this.setData({
      scrollLeft: scrollLeft
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    var that = this
    that.setData({
      wantMovieInfo: 'ele' + wx.getStorageSync('selectMovieId')
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})